package com.baozou.framework.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkUtil {
	
	public final static String D = "NetworkUtil";
	
	public final static int WIFI = 1;
	public final static int MOBILE = 2;
	public final static int NOCONNECTED = 3;
	public final static int NOAVAILABLE = 4;
	
	public static int getNetwork(Context context){
		int ret = 0;
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if(info !=null && info.isAvailable()){
			Log.d(D, "connect is available.");
			if(info.isConnected()){
				Log.d(D, "connect is connected.");
				String typeName = info.getTypeName().toLowerCase();
				if(typeName.equals("wifi")){
					Log.d(D, "connect is wifi");
					ret = NetworkUtil.WIFI;
				}else{
					String subTypeName = info.getExtraInfo().toLowerCase();
					Log.d(D, "connect is mobile: " + subTypeName);
					ret = NetworkUtil.MOBILE;
				}
			}else{
				Log.d(D, "connect is not connected.");
				ret = NetworkUtil.NOCONNECTED;
			}
		}else{
			Log.d(D, "connect is not available.");
			ret = NetworkUtil.NOAVAILABLE;
		}
		return ret;
	}
}
